package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UserBlogSpotSubscriptionPeer extends BasePeer{
	public static final String tableName = "_User_BlogSpot_Subscription";
	public static final String USERID = "userId";
	public static final String BLOGSPOTID = "blogSpotId";
	public static final String SUBSCRITPTIONTYPE = "subscritptionType";
	public static final String STARTDATE = "startDate";
	public static final String DISCRIPTION = "discription";
	public static final String PUBLICPOLICY = "publicPolicy";
	public static final String[] columnNames=new String[]{"userId","blogSpotId","subscritptionType","startDate","discription","publicPolicy"};
	public static UserBlogSpotSubscription getUserBlogSpotSubscriptionByPKs(Object blogSpotId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserBlogSpotSubscriptionPeer.tableName,UserBlogSpotSubscriptionPeer.columnNames," TRUE  AND `blogSpotId`=?  AND `userId`=? ",new Object[]{blogSpotId,userId,},0, 1,conn);
			UserBlogSpotSubscription userBlogSpotSubscription = new UserBlogSpotSubscription();
		fill(rs,userBlogSpotSubscription);
		return userBlogSpotSubscription;
	}
	public static UserBlogSpotSubscription getUserBlogSpotSubscriptionReferenceColumnsOnlyByPKs(Object blogSpotId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserBlogSpotSubscriptionPeer.tableName,new String[]{"blogSpotId","userId",}," TRUE AND `blogSpotId`=?AND `userId`=?",new Object[]{blogSpotId,userId,}, 0,  1,conn);
			UserBlogSpotSubscription userBlogSpotSubscription = new UserBlogSpotSubscription();
		fill(rs,userBlogSpotSubscription);
		return userBlogSpotSubscription;
	}
	public static UserBlogSpotSubscription getUserBlogSpotSubscriptionByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserBlogSpotSubscriptionPeer.tableName,UserBlogSpotSubscriptionPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			UserBlogSpotSubscription userBlogSpotSubscription = new UserBlogSpotSubscription();
		fill(rs,userBlogSpotSubscription);
		return userBlogSpotSubscription;
	}
	public static UserBlogSpotSubscription getUserBlogSpotSubscriptionByBlogSpotId(int blogSpotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserBlogSpotSubscriptionPeer.tableName,UserBlogSpotSubscriptionPeer.columnNames,"`blogSpotId`=?",new Object[]{blogSpotId},0, 1,conn);
			UserBlogSpotSubscription userBlogSpotSubscription = new UserBlogSpotSubscription();
		fill(rs,userBlogSpotSubscription);
		return userBlogSpotSubscription;
	}
	public static void fill(ResultSet rs, UserBlogSpotSubscription userBlogSpotSubscription) throws SQLException {
		if(rs.next()){
			try{userBlogSpotSubscription.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setBlogSpotId(rs.getInt("blogSpotId"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setSubscritptionType(rs.getInt("subscritptionType"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setStartDate(rs.getTimestamp("startDate"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setDiscription(rs.getString("discription"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setPublicPolicy(rs.getInt("publicPolicy"));}catch(Exception ex){}
			userBlogSpotSubscription.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<UserBlogSpotSubscription> userBlogSpotSubscriptionCollection) throws SQLException {
		while(rs.next()){
			UserBlogSpotSubscription userBlogSpotSubscription=new UserBlogSpotSubscription();
			try{userBlogSpotSubscription.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setBlogSpotId(rs.getInt("blogSpotId"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setSubscritptionType(rs.getInt("subscritptionType"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setStartDate(rs.getTimestamp("startDate"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setDiscription(rs.getString("discription"));}catch(Exception ex){}
			try{userBlogSpotSubscription.setPublicPolicy(rs.getInt("publicPolicy"));}catch(Exception ex){}
			userBlogSpotSubscription.resetModified();
			userBlogSpotSubscriptionCollection.add(userBlogSpotSubscription);
		}
	}
	public static boolean update(UserBlogSpotSubscription userBlogSpotSubscription, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UserBlogSpotSubscriptionPeer.columnNames.length; i++) {
			if (((UserBlogSpotSubscriptionBase)userBlogSpotSubscription).modified[i]) {
				columns.add(UserBlogSpotSubscriptionPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `_User_BlogSpot_Subscription` SET ");
		sql.append("WHERE TRUE AND `blogSpotId` = ? AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,userBlogSpotSubscription.get(column));
		}
		pstmt.setObject(columns.size()+1+0,userBlogSpotSubscription.get("blogSpotId"));
		pstmt.setObject(columns.size()+1+1,userBlogSpotSubscription.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(UserBlogSpotSubscription userBlogSpotSubscription, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (userBlogSpotSubscription.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `_User_BlogSpot_Subscription` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (userBlogSpotSubscription.get(column) != null) {
				pstmt.setObject(i + 1 + j, userBlogSpotSubscription.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<UserBlogSpotSubscription> getUserBlogSpotSubscriptionCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<UserBlogSpotSubscription> collection=new Vector<UserBlogSpotSubscription>();
		fill(rs,collection);
		return collection;
	}
}
